<?php
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Controller;
use think\Session;
use changpay\ChangPay;
use app\api\controller\Order;
use app\common\controller\order\Orderget;
class Buibuibui extends Controller {
	
	/**
	 * 操作1
	 */
    public function caozuo1(){
        $header = Request::instance()->header();
        $post = $this->request->param(true); 
        $userId =$header['userid'];
        $info =Db::table('xb_mem_info')->where('ID',$userId)->find();
		$data['MobNo'] = $post['mobile'];//手机号码
		$data['IDNo'] =  $info['IDCard']; //身份证件号码
		$data['CstmrNm'] =  $info['TrueName']; //姓名
		$data['BkAcctNo'] =$post['bank_numer']; //卡号
		$data['MerUserId']=$userId;//用户ID
		$data['PartnerId'] =Db::table('xb_sys_inteparameter')->where(['ParaName'=>'changpay'])->value('ParaValue');//商户号 
        $changpay= new \changpay\ChangPay;
        $datas=$changpay->bank1($data);
//		p($datas);die;
		if($datas['AcceptStatus'] == "S"){
			show_success_api($datas['TrxId']);
		}else{
			show_error_api($datas['RetMsg']);
		}
    
		
		
		
		
		
    }
	/**
	 * 操作2
	 */
	 public function caozuo2(){
	 	
        $header = Request::instance()->header();
        $post = $this->request->param(true); 
        $userId = $header['userid'];
		$data['BankName'] =$post['BankName'];//银行卡名称   
        $data['OriAuthTrxId'] =$post['TrxId'];//获取验证码 返回的唯一识别码
		$data['SmsCode'] = $post['verify'];//验证码
		$data['PartnerId'] =Db::table('xb_sys_inteparameter')->where(['ParaName'=>'changpay'])->value('ParaValue');//商户号 
//		绑定银行卡时间先看看以前有没有已经绑定的银行卡   如果有就先解除绑定  然后再操作
		$renzheng_bank = Db::table('xb_renzen_bank')->where(['UserID'=>$userId,'Status'=>'1'])->find();//获取银行卡信息
		$changpay= new \changpay\ChangPay;
//		p($renzheng_bank);die;
//		
//		p($data);die;
		$datas=$changpay->bank2($data);
		if($datas['AcceptStatus'] == "S"){
			$update['BankNo']=$post['BankNo'];//卡号
			$update['BankName']=$post['BankName'];//银行卡名称
			$update['YMobile']=$post['YMobile'];//手机号
			if($renzheng_bank){
				 $update['UpdateTime']=date("Y-m-d H:i:s");
				 $result = Db::table('xb_renzen_bank')->where('UserID',$userId)->update($update);
				 $re_jiebang=$changpay->jiebang($renzheng_bank['BankNo'] ,$userId,$data['PartnerId']);
			}else{
				$update['RenzTime']=date("Y-m-d H:i:s");
				$update['UserID']=$userId;//用户ID
				$ret = Db::table('xb_renzen_bank')->insertGetId($update);
				if($ret){
//					修改用户表数据
			        $result = Db::table('xb_mem_info')->where('ID',$userId)->update(['isBank'=>'1']);
				}
			}
			show_success_api($datas['RetMsg']);
			
		}else{
			show_error_api($datas['RetMsg']);
		}

    
	 
	 }
public function caozuo3(){
	
        $this->logs('管理员放款');
        $data = input();
        $LoanStatus = Db::table('xb_loans_applylist')->where(['ID' => $data['ID']])->value('LoanStatus');
        if ($LoanStatus != 1) {
            show_error('', $message = '订单不予许操作');
        }
        $Payceshi = Db::table('xb_sys_basicinfo')->value('Payceshi');
        $Orderget = new Orderget();
        $Payceshi = $Orderget->get_Payceshi(); //获取支付环境
        if ($Payceshi == '1') {
            $data['TransAmt'] = $data['AdoptMoney']; //放款金额
        } else {
            $data['TransAmt'] = '0.02'; //放款金额
        }
        $data['PartnerId'] = $Orderget->get_changpay(); //获取商户号
        $data['AcctNo'] = $data['BankNo']; //名字
        $data['AcctName'] = $data['TrueName']; //卡号
        $data['BankCommonName'] = $data['BankName']; //卡号
        if ($data['pay'] == '3') {//调用银行卡支付
          $changpay = new \changpay\ChangPay();
            $datas = $changpay->payment($data);
            if ($datas['AcceptStatus'] == 'S') {
                $data_update['ID'] = $data['ID'];
                $data_update['OutTradeNo'] = $datas['OutTradeNo']; //支付订单号
            $data_update['LoanStatus'] = '6'; //订单状态
            $data_update['pay'] = $data['pay']; //支付方式
            $data_update['FkadminID'] = session('id'); //放款人员ID
            }
        } else {//支付宝或者微信放款
            $days = Db::table('xb_loans_applylist')->where(['ID' => $data['ID']])->value('ApplyDay'); //根据订单ID获取放款时间
            $data_update['ID'] = $data['ID'];
            $data_update['LoanStatus'] = '2'; //订单状态
            $data_update['pay'] = $data['pay']; //支付方式
            $data_update['OpenTime'] = date('Y-m-d H:i:s'); //放款时间
            $YyFkTime = $Orderget->get_time(date('Y-m-d'), $days); //获取预约还款时间
            $data_update['YyFkTime2'] = $YyFkTime; //预约还款时间  初始时间不会变
            $data_update['YyFkTime'] = $YyFkTime; //预约还款时间   续期增加
            $data_update['FkadminID'] = session('id'); //放款人员ID
        }
        $re = Db::table('xb_loans_applylist')->update($data_update);
        if ($re) {
            if ($data['pay'] != '3') {
                //次数发送 微信支付宝放款的短信
                //获取短信模板与用户信息组合成短信模板发送短信通知
                $smsTemp = (new SmsTemplate())->getSmsInfo('loan_inform');
                $smsTempStr = str_replace(['#name#', '#suffix#', '#refund_time#'], [$data['TrueName'], substr($data['BankNo'], -4), $data_update['YyFkTime']], $smsTemp['Content']);

                //发送短信
                $result1 = (new CLApi())->sendSMS($data['Mobile'], $smsTempStr);
                $result2 = json_decode($result1, true);

                //写入对接产品记录表
                (new Docking())->addDocking(1, ($result2['code'] == 0) ? 2 : 3, 'chuanglan', '订单放款短信通知', $result1);

                if ($result2['code'] != 0) {
                    \Think\Log::record('放款操作 发送短信失败 数据 - '.$result1);
                }
            }
            show_success();
        } else {
            show_error();
        }
    
	
	
}


	 
	 
	 



}
